/*
* @(#)LotusDominoNotification.java
*
* Copyright (c) 2003 DCIVision Ltd
* All rights reserved.
*
* This software is the confidential and proprietary information of DCIVision
* Ltd ("Confidential Information").  You shall not disclose such Confidential
* Information and shall use it only in accordance with the terms of the license
* agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.framework.notification;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.SystemParameterConstant;
import com.dcivision.framework.SystemParameterFactory;
import com.dcivision.framework.Utility;
/**
 * <p>Class Name:       EmailNotification.java    </p>
 * <p>Description:      This class implements NotificationWay, is used to initiate </p>
 * <p>                  an EmailDeliveryMessage object, and get the email SMTP host information</p>
 * <p>                  to send the resolved email message. </p>
 *
 * @author           Zoe Shum
 * @company          DCIVision Limited
 * @creation date    02/07/2003
 * @version          $Revision: 1.4.2.1 $
 */

public class LotusDominoNotification implements NotificationWay {

  public static final String REVISION = "$Revision: 1.4.2.1 $";

  private Log log = LogFactory.getLog(this.getClass().getName());

  private List m_lMsgList;
  private boolean m_bDebug = false;

  /**
   *  Constructor - initiate the global variable
   *
   *  @param      No pass in parameter
   *  @return     No return value
   *  @throws     No exception throws
   */
  public LotusDominoNotification() {
    m_lMsgList = new ArrayList();
  }

  /**
   *  Method addDeliveryMessage() - add the object DeliveryMessage in email
   *
   *  @param      message                 The detail message info in object DeliveryMessage
   *  @return     void
   *  @throws     No exception throws
   */
  public void addDeliveryMessage(DeliveryMessage message) {
    m_lMsgList.add(message);
  }

  /**
   *  Method setDebug() - set the boolean value for debug flag
   *
   *  @param      bDebug                   The boolean value of debug flag
   *  @return     void
   *  @throws     No exception throws
   */
  public void setDebug(boolean bDebug) {
    this.m_bDebug = bDebug;
  }

  /**
   *  Method getDebug() - get the boolean value for debug flag
   *
   *  @param      No pass in parameter
   *  @return     boolean                   The boolean value of debug flag
   *  @throws     No exception throws
   */
  public boolean getDebug() {
    return m_bDebug;
  }

  /**
   *  Method send() - trigger the mailer
   *  It base on the SMTP_MAX_THREAD_COUNT to determine to send the email in synchronous mode or asynchronous mode
   *
   *  @param      No pass in parameter
   *  @return     void
   *  @throws     ApplicationException      Throws ApplicationException if either cannot connect the SMTP host,
   *                                        or cannot resolve the message content
   */
  public boolean send() throws ApplicationException {
    String smtpMaxThread = SystemParameterFactory.getSystemParameter(SystemParameterConstant.SMTP_MAX_THREAD_COUNT);
    if (!Utility.isEmpty(smtpMaxThread) && Integer.parseInt(smtpMaxThread) > 0) {
      //new a mailer thread and put that thread into the work queue
	    MailerThread mailerThread = new MailerThread(m_lMsgList);
	    mailerThread.setDebug(m_bDebug);
	    EmailBackgroundWorkQueue.getInstance().execute(mailerThread);
    } else {
	    //send out the mail directly, synchronous mode
	    LotusDominoMailer mailer = new LotusDominoMailer();
	    mailer.setDebug(m_bDebug);
      mailer.sendEmail(m_lMsgList);
    }
    return true;
  }

  /**
   * Helper class for threading the mailer
   */
  class MailerThread extends Thread{
    boolean mDebug = false;
    List mMessages = new ArrayList();

    public MailerThread(List messages){
      mMessages = messages;
    }

    public void setDebug(boolean debug){
	    mDebug = debug;
    }

    /**
     * Start the thread and trigger the real Mailer
     */
    public void run(){
      try{
        LotusDominoMailer mailer = new LotusDominoMailer();
        mailer.setDebug(mDebug);
        mailer.sendEmail(mMessages);
      }catch(ApplicationException e){
        log.error("Fail to send email out:"+e.getMessage());
      }
    }
  }
}